package com.jy.rhin.infra.cda.util;

import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: lww
 * @Date: 2022/8/12 16:05
 */
public class ZhiJiUtils {

    private ZhiJiUtils() {
    }

    private static final String VERTICAL = "|";
    private static final String VERTICAL_TRANSLATION = "\\|";

    /**
     * 根据值集标识符获取值名称
     *
     * @param zhiJiBSF
     * @param code
     * @param map
     * @return
     */
    public static String getValue(String zhiJiBSF, String code, Map<String, List<DaiMaDto>> map) {
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        List<DaiMaDto> daiMaDtoList = null;
        for (Map.Entry<String,List<DaiMaDto>> entry : map.entrySet()) {
            String key = entry.getKey();
            if (StringUtils.pathEquals(key.toLowerCase(Locale.ROOT), zhiJiBSF.toLowerCase(Locale.ROOT))) {
                daiMaDtoList = entry.getValue();
            }
        }
        if (CollectionUtils.isEmpty(daiMaDtoList)) {
            return code;
        }
        if (code.contains(VERTICAL)) {
            String[] split = code.split(VERTICAL_TRANSLATION);
            List<String> codeList = Arrays.asList(split);
            return getNameList(codeList, daiMaDtoList);
        } else {
            return getName(code, daiMaDtoList);
        }
    }

    /**
     * 字段单条记录处理
     *
     * @param code
     * @param daiMaDtoList
     * @return
     */
    private static String getName(String code, List<DaiMaDto> daiMaDtoList) {
        return daiMaDtoList.stream().filter(x -> x.getCode().equals(code)).findFirst().map(DaiMaDto::getValue).orElse(code);
    }

    /**
     * 字段多条记录处理
     *
     * @param code
     * @param daiMaDtoList
     * @return
     */
    private static String getNameList(List<String> code, List<DaiMaDto> daiMaDtoList) {
        Map<String, String> codeMap = daiMaDtoList.stream().collect(Collectors.toMap(DaiMaDto::getCode, DaiMaDto::getValue));
        List<String> nameList = code.stream().filter(codeMap::containsKey).map(codeMap::get).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(nameList)) {
            return String.join("、", code);
        }
        return String.join("、", nameList);
    }

    /**
     * 添加值集标识符和对应code
     * @param zhiJiBSFList
     * @param zhiJiBSF
     * @param codeSet
     */
    public static void addValueItem(List<ZhiYuMXCXDto> zhiJiBSFList, String zhiJiBSF, Set<String> codeSet){
        if(!StringUtils.isEmpty(zhiJiBSF) && !CollectionUtils.isEmpty(codeSet)) {
            final ZhiYuMXCXDto[] dto = {new ZhiYuMXCXDto()};
            dto[0].setZhiJiBSF(zhiJiBSF);
            if(zhiJiBSFList.contains(dto[0])){
                zhiJiBSFList.stream().filter(obj -> obj.getZhiJiBSF().equals(zhiJiBSF)).findFirst().ifPresent(item -> dto[0] = item );
                zhiJiBSFList.remove(dto[0]);
                codeSet.addAll(dto[0].getCodeList());
            }
            List<String> codeList = new ArrayList<>(codeSet);
            dto[0].setCodeList(codeList);
            zhiJiBSFList.add(dto[0]);
        }
    }

}
